package com.epiboly.base.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.epiboly.base.model.bo.LoginUserBo;
import com.epiboly.base.util.HttpSessionUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * MybatisPlusMetaObjectHandler
 *
 * @author liangxu
 */
@Slf4j
@Component
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        LoginUserBo loginUser = HttpSessionUtils.getLoginUser();
        this.strictInsertFill(metaObject, "creatorId", Long.class, loginUser.getId());
        this.strictInsertFill(metaObject, "creatorName", String.class, loginUser.getName());
        this.strictInsertFill(metaObject, "created", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updaterId", Long.class, loginUser.getId());
        this.strictInsertFill(metaObject, "updaterName", String.class, loginUser.getName());
        this.strictInsertFill(metaObject, "updated", LocalDateTime.class, LocalDateTime.now());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        LoginUserBo loginUser = HttpSessionUtils.getLoginUser();
        this.strictInsertFill(metaObject, "updaterId", Long.class, loginUser.getId());
        this.strictInsertFill(metaObject, "updaterName", String.class, loginUser.getName());
        this.strictInsertFill(metaObject, "updated", LocalDateTime.class, LocalDateTime.now());
    }
}